package cn.tedu.tea.product.controller;

import cn.tedu.tea.product.pojo.dto.ContentAddNewDTO;
import cn.tedu.tea.product.pojo.dto.ContentDetailAddNewDTO;
import cn.tedu.tea.product.pojo.dto.ContentDetailUpdateDTO;
import cn.tedu.tea.product.pojo.dto.ContentUpdateDTO;
import cn.tedu.tea.product.pojo.vo.ContentDetailListVO;
import cn.tedu.tea.product.pojo.vo.ContentDetailStandardVO;
import cn.tedu.tea.product.pojo.vo.ContentListVO;
import cn.tedu.tea.product.pojo.vo.ContentStandardVO;
import cn.tedu.tea.product.service.IContentDetailService;
import cn.tedu.tea.product.service.IContentService;
import cn.tedu.tea.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/content")
@Api(tags = "01. 内容管理模块")
public class ContentController {
    @Autowired
    private IContentService contentService;
    @Autowired
    private IContentDetailService detailService;

    public ContentController() {
        log.debug("创建控制器对象：ContentController");
    }

    @ApiOperation("添加内容")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(@Valid ContentAddNewDTO contentAddNewDTO
                             ,@Valid ContentDetailAddNewDTO detailAddNewDTO
    ) {
        log.debug("开始处理【添加内容】的请求，参数：{}", contentAddNewDTO);
        contentService.addNew(contentAddNewDTO,detailAddNewDTO);
        log.debug("添加内容成功！");
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除内容")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "内容id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@Range(min = 1, message = "删除内容失败，尝试删除的内容的ID无效！")
                                   @PathVariable Integer id) {
        log.debug("开始处理【根据id删除内容】的请求，参数：{}", id);
        contentService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改内容详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "内容id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult updateById(@PathVariable Integer id, ContentUpdateDTO contentUpdateDTO,
                                 ContentDetailUpdateDTO detailUpdateDTO) {
        log.debug("开始处理【修改内容详情】的请求，参数ID：{}, 新数据：{}", id, contentUpdateDTO);
        contentService.updateInfoById(id, contentUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据id查询内容详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "内容id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<ContentStandardVO> getStandardById(@PathVariable Integer id) {
        log.debug("开始处理【根据id查询内容详情】的请求，参数：{}", id);
        ContentStandardVO categoryStandardVO = contentService.getStandardById(id);
//        ContentDetailStandardVO detailStandardVO = detailService.getStandardById(id);
        return JsonResult.ok(categoryStandardVO);
    }

    @ApiOperation("查询内容列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<ContentListVO>> list() {
        log.debug("开始处理【查询内容列表】的请求，无参数");
        List<ContentListVO> list = contentService.list();
        return JsonResult.ok(list);
    }
}
